package com.yc.yyds.servlet;

import com.google.gson.Gson;
import com.yc.yyds.biz.UserLoginBiz;
import com.yc.yyds.Bean.Result;
import com.yc.yyds.util.SendSms;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Map;

@WebServlet(name = "UserPhoneLoginServlet", value = "/UserPhoneLoginServlet.s")
public class UserPhoneLoginServlet extends HttpServlet {
    UserLoginBiz biz =new UserLoginBiz();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取接受验证码的手机
        String phone = request.getParameter("phone");
        //获取生成的随机验证码
        String code = SendSms.suijiNum(4);
        Result result;
        try {
            Map<String, Object> data = biz.userPhoneLogin(phone);
            if (data!=null){
                request.getSession().setAttribute("userMsg",data);
            }
            //发送短信
            SendSms.SendSMSByAli(phone,code);
            data.put("code",code);
            result=new Result(1,"登录成功",data);
            String json = new Gson().toJson(result);
            response.getWriter().append(json);
        } catch (Exception e) {
            System.out.println(e.getMessage());


            result=new Result(0,e.getMessage(),null);
            String json = new Gson().toJson(result);
            response.getWriter().append(json);
        }

    }
}
